package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.enums.OrderType;
import com.ruoyi.system.domain.TudaOrderTransfer;
import com.ruoyi.system.model.param.EditOrderOtherFee;
import com.ruoyi.system.model.param.EditOrderParam;
import com.ruoyi.system.service.ISysUserService;
import org.apache.commons.math3.stat.correlation.Covariance;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TudaOrder;
import com.ruoyi.system.service.ITudaOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 货运订单Controller
 *
 * @author tuda
 * @date 2023-07-14
 */
@Controller
@RequestMapping("/system/order")
public class TudaOrderController extends BaseController
{
    private String prefix = "system/order";

    @Resource
    private ITudaOrderService tudaOrderService;
    @Resource
    private ISysUserService sysUserService;


    @RequiresPermissions("system:order:detail")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        TudaOrder tudaOrder = tudaOrderService.selectTudaOrderById(id);
        String[] str = tudaOrder.getCarImgs().split(",");
        List<String> carList = new ArrayList<>();
        for (int i = 0; i < str.length; i++) {
            carList.add(str[i]);
        }
        tudaOrder.setCarImgList(carList);
        String[] str2 = tudaOrder.getContractImgs().split(",");
        List<String> contractImgsList = new ArrayList<>();
        for (int i = 0; i < str2.length; i++) {
            contractImgsList.add(str2[i]);
        }
        tudaOrder.setContractImgList(contractImgsList);
        mmap.put("tudaOrder", tudaOrder);
        return prefix + "/detail";
    }

    @RequiresPermissions("system:order:view")
    @GetMapping()
    public String order()
    {
        return prefix + "/order";
    }

    /**
     * 查询货运订单列表
     */
    @RequiresPermissions("system:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TudaOrder tudaOrder)
    {
        startPage();
        tudaOrder.setIsDelete(0);
        List<TudaOrder> list = tudaOrderService.selectTudaOrderList(tudaOrder);
        return getDataTable(list);
    }

    @RequiresPermissions("system:order:list")
    @PostMapping("/transferList")
    @ResponseBody
    public List<TudaOrderTransfer> transferList(Long orderId)
    {
        List<TudaOrderTransfer> list = tudaOrderService.transferList(orderId);
        return list;
    }

    @RequiresPermissions("system:order:two")
    @GetMapping("/two")
    public String orderTwo()
    {
        return prefix + "/orderTwo";
    }

    @RequiresPermissions("system:order:listTwo")
    @PostMapping("/listTwo")
    @ResponseBody
    public TableDataInfo listTwo(TudaOrder tudaOrder)
    {
        startPage();
        tudaOrder.setOrderType(OrderType.ORDER_3.getCode());
        tudaOrder.setIsDelete(0);
        List<TudaOrder> list = tudaOrderService.selectTudaOrderList(tudaOrder);
        return getDataTable(list);
    }

    @RequiresPermissions("system:order:delete")
    @GetMapping("/delete")
    public String deleteList()
    {
        return prefix + "/deleteOrder";
    }

    /**
     * 查询货运订单列表
     */
    @RequiresPermissions("system:order:deleteList")
    @PostMapping("/deleteList")
    @ResponseBody
    public TableDataInfo deleteList(TudaOrder tudaOrder)
    {
        startPage();
        tudaOrder.setIsDelete(1);
        List<TudaOrder> list = tudaOrderService.selectTudaOrderList(tudaOrder);
        return getDataTable(list);
    }

    /**
     * 导出货运订单列表
     */
    @RequiresPermissions("system:order:export")
    @Log(title = "货运订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TudaOrder tudaOrder)
    {
        List<TudaOrder> list = tudaOrderService.selectTudaOrderList(tudaOrder);
        ExcelUtil<TudaOrder> util = new ExcelUtil<TudaOrder>(TudaOrder.class);
        return util.exportExcel(list, "货运订单数据");
    }

    /**
     * 新增货运订单
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("userList", sysUserService.queryUserByRole());
        return prefix + "/add";
    }

    /**
     * 新增保存货运订单
     */
    @RequiresPermissions("system:order:add")
    @Log(title = "货运订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated TudaOrder tudaOrder)
    {
        return toAjax(tudaOrderService.insertTudaOrder(tudaOrder));
    }

    /**
     * 修改货运订单
     */
    @RequiresPermissions("system:order:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TudaOrder tudaOrder = tudaOrderService.selectTudaOrderById(id);
        String[] str = tudaOrder.getCarImgs().split(",");
        List<String> carList = new ArrayList<>();
        for (int i = 0; i < str.length; i++) {
            carList.add(str[i]);
        }
        tudaOrder.setCarImgList(carList);
        String[] str2 = tudaOrder.getContractImgs().split(",");
        List<String> contractImgsList = new ArrayList<>();
        for (int i = 0; i < str2.length; i++) {
            contractImgsList.add(str2[i]);
        }
        tudaOrder.setContractImgList(contractImgsList);
        mmap.put("userList", sysUserService.queryUserByRole());
        mmap.put("tudaOrder", tudaOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存货运订单
     */
    @RequiresPermissions("system:order:edit")
    @Log(title = "货运订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TudaOrder tudaOrder)
    {
        return toAjax(tudaOrderService.updateTudaOrder(tudaOrder));
    }

    /**
     * 修改货运订单
     */
    @RequiresPermissions("system:order:renew")
    @GetMapping("/renew/{id}")
    public String renew(@PathVariable("id") Long id, ModelMap mmap)
    {
        TudaOrder tudaOrder = tudaOrderService.selectTudaOrderById(id);
        String[] str = tudaOrder.getCarImgs().split(",");
        List<String> carList = new ArrayList<>();
        for (int i = 0; i < str.length; i++) {
            carList.add(str[i]);
        }
        tudaOrder.setCarImgList(carList);
        String[] str2 = tudaOrder.getContractImgs().split(",");
        List<String> contractImgsList = new ArrayList<>();
        for (int i = 0; i < str2.length; i++) {
            contractImgsList.add(str2[i]);
        }
        tudaOrder.setContractImgList(contractImgsList);
        mmap.put("userList", sysUserService.queryUserByRole());
        mmap.put("tudaOrder", tudaOrder);
        return prefix + "/renew";
    }

    /**
     * 修改保存货运订单
     */
    @RequiresPermissions("system:order:renew")
    @Log(title = "货运订单", businessType = BusinessType.UPDATE)
    @PostMapping("/renew")
    @ResponseBody
    public AjaxResult renewSave(TudaOrder tudaOrder)
    {
        return toAjax(tudaOrderService.updateTudaOrder(tudaOrder));
    }

    /**
     * 删除货运订单
     */
    @RequiresPermissions("system:order:update")
    @Log(title = "更新运单状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/update")
    @ResponseBody
    public AjaxResult updateStatus(String ids, Integer type)
    {
        return toAjax(tudaOrderService.updateStatusByIds(ids, type));
    }

    @RequiresPermissions("system:order:update")
    @PostMapping( "/updateOrderType")
    @ResponseBody
    public AjaxResult updateOrderType(String ids)
    {
        return toAjax(tudaOrderService.updateOrderTypeByIds(ids));
    }

    /**
     * 删除货运订单
     */
    @RequiresPermissions("system:order:remove")
    @Log(title = "删除运单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tudaOrderService.deleteTudaOrderByIds(ids));
    }

    @RequiresPermissions("system:order:transport")
    @GetMapping("/transport/{id}")
    public String transport(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("orderList", tudaOrderService.queryOrderById(id));
        mmap.put("id", id);
        return prefix + "/transport";
    }

    @RequiresPermissions("system:order:transport")
    @PostMapping( "/transport")
    @ResponseBody
    public AjaxResult transportSave(EditOrderParam orderTransfer)
    {
        return toAjax(tudaOrderService.transportSave(orderTransfer));
    }

    @RequiresPermissions("system:order:updateOtherFee")
    @PostMapping( "/updateOtherFee")
    @ResponseBody
    public AjaxResult updateOtherFee(EditOrderOtherFee orderFee)
    {
        return toAjax(tudaOrderService.updateOtherFee(orderFee));
    }

    /**
     * 新增中转
     */
    @GetMapping("/addTransfer/{id}")
    public String addTransfer(@PathVariable("orderId") String orderId, ModelMap mmap) {
        mmap.put("orderId", orderId);
        return prefix + "/addTransfer";
    }

    /**
     * 新增保存中转
     */
    @RequiresPermissions("system:order:add")
    @Log(title = "货运订单", businessType = BusinessType.INSERT)
    @PostMapping("/addTransfer")
    @ResponseBody
    public AjaxResult addTransferSave(@Validated TudaOrderTransfer orderTransfer)
    {
        return toAjax(tudaOrderService.addOrderTransfer(orderTransfer));
    }
}
